/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.tree;

import java.util.ArrayList;
import java.util.List;

import org.rails.extjs.Observable;

public abstract class Node extends Observable {
	
	protected final List<Node> childNodes = new ArrayList<Node>();
	protected final List<Node> childrens = new ArrayList<Node>();
	
	public Node(){
		super();
	}
	
	public Node(String id) {
		super();
		setId(id);
	}
	
	public void setId(String id){
		if(id != null && !id.equals(""))
			addOption("id",id);
	}
	
	public String getId(){
		return getString("id","");
	}
	
	public void setLeaf(Boolean leaf){
		addOption("leaf", leaf);
	}
	
	public Boolean isLeaf(){
		return getBoolean("leaf",false);
	}
	
	public void addChildNode(Node childNode){
		childNodes.add(childNode);
	}
	
	public void insertChildNode(int index,Node childNode){
		childNodes.add(index, childNode);
	}
	
	public void removeChildNode(int index){
		childNodes.remove(index);
	}
	
	public void clearChildNode(int index){
		childNodes.clear();
	}
	
	public List<Node> getChildNodes(){
		return childNodes;
	}
	
	public void setAttributes(Object attributes){
		addOption("attributes",attributes);
	}
	
	public void addChildren(Node children){
		childrens.add(children);
	}
	
	public void insertChildren(int index,Node children){
		childrens.add(index, children);
	}
	
	public void removeChildren(int index){
		childrens.remove(index);
	}
	
	public Node getChildren(int index){
		 return childrens.get(index);
	}
	
	public void clearChildren(){
		childrens.clear();
	}
	
	public List<Node> getChildrens(){
		return childrens;
	}
	
	public Object getAttributes(){
		return getOption("attributes");
	}
	
	public void setFirstChild(Node firstChild){
		addOption("firstChild",firstChild);
	}
	
	public Node getFirstChild(Node firstChild){
		return (Node)getOption("firstChild");
	}
	
	public void setLastChild(Node lastChild){
		addOption("lastChild",lastChild);
	}
	
	public Node getLastChild(Node lastChild){
		return (Node)getOption("lastChild");
	}
	
	public void setNextSibling(Node nextSibling){
		addOption("nextSibling",nextSibling);
	}
	
	public Node getNextSibling(){
		return (Node)getOption("nextSibling");
	}
	
	public void setParentNode(Node parentNode){
		addOption("parentNode", parentNode);
	}
	
	public Node getParentNode(){
		return (Node)getOption("parentNode");
	}
	
	public void setPreviousSibling(Node previousSibling){
		addOption("previousSibling",previousSibling);
	}
	
	public Node getPreviousSibling(){
		return (Node)getOption("previousSibling");
	}
	
	@Override
	protected void createItems(){
		super.createItems();
		if(childNodes.size() > 0)
			addOption("childNodes", childNodes);
		if(childrens.size() > 0)
			addOption("children", childrens);
	}

}
